<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class CreateOrderProductItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('order_product_items')) {
            Schema::create('order_product_items', function (Blueprint $table) {
                $table->id();
                $table->Integer('order_id')->comment('订单 ID');
                $table->string('order_num')->comment('订单编号');
                $table->Integer('product_id')->comment('商品 ID');
                $table->Integer('spec_id')->comment('商品规格 ID');
                $table->Integer('buy_num')->comment('购买数量');
                $table->integer('weight')->comment('单位重量,单位:克');
                $table->integer('total_weight')->comment('总重量,单位:克');
                $table->string('unit')->comment('商品单位');
                $table->decimal('unit_price',10,2)->comment('商品单价:单位分,保留两位小数点');
                $table->decimal('total_price',14,2)->comment('商品总价');
                $table->decimal('actual_unit_price',10,2)->comment('商品实际单价单位分,保留两位小数点');
                $table->decimal('actual_total_price',14,2)->comment('商品实际总价');
                $table->integer('discount_price')->default(0)->comment('优惠金额');
                $table->string('product_name')->comment('历史数据:商品名称');
                $table->string('spec_name')->comment('历史数据:商品规格名称');
                $table->json('spec_attributes')->nullable()->comment('历史数据:商品规格属性');
                $table->string('catname')->comment('历史数据:商品分类名称');
                $table->string('brand_name')->comment('历史数据:商品品牌名称');
                $table->string('product_image')->comment('历史数据:商品图片');
                $table->string('product_image_thumb')->comment('历史数据:商品缩略图');
                $table->index(['order_id', 'order_num']);
                $table->softDeletes();
                $table->timestamps();
                $table->index(['order_id']);
            });
            DB::statement("ALTER TABLE `order_product_items` comment '订单商品明细表'");
        }
        
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order_product_items');
    }
}
